/************************************************************
 * Filename: Sprite.cpp
 * Purpose: Defines the Sprite component. This is the main
 * visual representation of a game object, if it has one.
 * Author: Jeremy Kings
 * Revision: 1 (30 November 2010)
 *
 * Copyright 2010, Digipen Institute of Technology
 ************************************************************/
#include "Precompiled.h"
#include "Sprite.h"
#include "Graphics.h"
#include "DebugDrawer.h"

namespace Engine
{
	void Sprite::Update(System* system, float frameTime)
	{
		// If object was damaged recently, alternate between drawing and not drawing 
		// (i.e. create a sprite flicker effect)
		if (GetOwner()->Timer > 0.0)
		{
			GetOwner()->Timer -= frameTime;
			if(GetOwner()->Timer > (floor(GetOwner()->Timer) + 0.5f)) return;
		}

		Graphics* pGraphics = (Graphics*)system;

		// Compute the scaling matrix
		Matrix4 transform = Matrix4Scale(2*GetOwner()->GetScale(),2*GetOwner()->GetScale(), 1.0f);
		// Compute the rotation matrix, concatenate it
		transform *= Matrix4RotZRad(GetOwner()->Direction);
		// Compute the translation matrix, concatenate it 
		transform *= Matrix4Translate(GetOwner()->Position.x,GetOwner()->Position.y, 0.0f);
		
		pGraphics->pDevice->SetTransform(D3DTS_WORLD,&transform);
		
		pGraphics->pDevice->SetTexture(0,pGraphics->Textures[GetOwner()->GetType()]);
		
		// Draw!
		pGraphics->pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
	}
}
